﻿<%@ Page CodeFileBaseClass="AliCMS.Lib.SystemPageBase" %>
<ali:Get ID="Rule" Object="Rule" InnerID="Request.QueryString['ID']" />
<script language="c#" runat="server">
protected DataTable _fields;
protected DataTable Fields
{
	get
	{
		if (_fields == null)
		{
			if (Rule[Alias.Rule.Config].ToString() != String.Empty)
			{
				XmlDocument config = new XmlDocument();
				config.LoadXml(Rule[Alias.Rule.Config].ToString());
                XmlNodeList fieldNodes = config.DocumentElement.SelectNodes("Fields/Field");
                foreach (XmlNode fieldNode in fieldNodes)
                {
                    if (fieldNode.Attributes["Name"] == null || fieldNode.Attributes["IsDisplay"] == null || fieldNode.Attributes["IsDisplay"].Value != "1")
                        continue;

					if (_fields == null)
					{
						_fields = new DataTable();
						_fields.Columns.Add("Name", typeof(string));
					}
					
					DataRow row = _fields.NewRow();
	                row["Name"] = fieldNode.Attributes["Name"].Value;
					_fields.Rows.Add(row);
                }
			}
		}
		
		return _fields;
	}
}
protected string BuildFields(object record)
{
	if (Fields == null)
		return String.Empty;

	StringBuilder result = new StringBuilder();
	foreach (DataRow field in Fields.Rows)
	{
		string value = HttpUtility.HtmlEncode(((DataRow)record)[field[0].ToString()].ToString());
		if (value.StartsWith("http://"))
			value = "<a href=\"" + value + "\" target=\"_blank\">" + value + "</a>";
		
		result.Append("<td>" + value + "</td>");
	}
	
	return result.ToString();
}

protected string Condition()
{
	StringBuilder condition = new StringBuilder();
	string and = String.Empty;

	#region 规则ID
	condition.Append(and + "[RuleID]={" + Rule[Alias.Rule.ID] + "}");
	#endregion

	if (and == String.Empty && condition.Length != 0)
		and = " AND ";

	#region 关键词
	string keyword = Request.QueryString["Keyword"];
	if (!String.IsNullOrEmpty(keyword))
	{
		condition.Append(and + "[" + Request.QueryString["KeywordType"] + "] LIKE '%" + keyword + "%'");
	}
	#endregion
	
	return condition.ToString();
}

protected string Sort()
{
	string sort = String.Empty;
	switch (Request.QueryString["Sort"])
	{
		case "1":
			sort = "[CreateTime] DESC";
			break;
		case "2":
			sort = "[CreateTime] ASC";
			break;
	}
	
	return sort;
}
</script>
<table border="0" cellpadding="4" cellspacing="1" class="ListTable">
	<thead>
		<tr>
			<th width="40">选择</th>
			<th width="40">编号</th>
			<th>创建时间</th>
			<ali:List Source="Fields">
				<ali:Record>
					<th><%=[0]%></th>
				</ali:Record>
			</ali:List>
		</tr>
	</thead>
	<tbody>
		<ali:List ID="List" Object="Rule.Result" Page="PageNumber" PageSize="20" Condition="Condition()" Sort="Sort()" Validate="True">
			<ali:Record>
				<tr>
					<td><input type="checkbox" name="Sel" value="<%=ID%>" /></td>
					<td><%=ID%></td>
					<td><%=CreateTime%></td>
					<%=BuildFields(Record)%>
				</tr>
			</ali:Record>
		</ali:List>
	</tbody>
</table>
<ali:Pagination Page="PageNumber" PageSize="20" RecordCount="List[RecordCount]">
	<div class="Pagination">
		<div class="Left">共 <%=RecordCount%> 条 当前显示第 <%=PageNumber%> 页 第 <%=RangeLower%>-<%=RangeUpper%> 条 </div>
		<div class="Right">
			<ali:FirstPage>
				<ali:Yes>
					<img src="<%=ImagePath%>pagination/first_page_off.gif"/>
					<img src="<%=ImagePath%>pagination/previous_page_off.gif"/>
				</ali:Yes>
				<ali:No>
					<a href="javascript:Go(1)"><img src="<%=ImagePath%>pagination/first_page_on.gif"/></a>
					<a href="javascript:Go(<%=PageNumber%> - 1)" id="PrevPage"><img src="<%=ImagePath%>pagination/previous_page_on.gif"/></a>
				</ali:No>
			</ali:FirstPage>
			<ali:CurrentPage Range="5">
				<ali:Yes>
					<span class="Current"><%=Index%></span>						
				</ali:Yes>
				<ali:No>
					<a href="javascript:Go(<%=Index%>)">[<%=Index%>]</a>
				</ali:No>
			</ali:CurrentPage>
			<ali:LastPage>
				<ali:Yes>
					<img src="<%=ImagePath%>pagination/next_page_off.gif"/>
					<img src="<%=ImagePath%>pagination/last_page_off.gif"/>
				</ali:Yes>
				<ali:No>
					<a href="javascript:Go(<%=PageNumber%> + 1)" id="NextPage"><img src="<%=ImagePath%>pagination/next_page_on.gif"/></a>
					<a href="javascript:Go(<%=PageCount%>)"><img src="<%=ImagePath%>pagination/last_page_on.gif"/></a>
				</ali:No>
			</ali:LastPage>
		</div>
	</div>
</ali:Pagination>
